// subcontract/Home/othersUser/othersUser.js
// import { navigateBack } from '../../../utils/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    flag: false,
    tabs: ['采购信息', '货源信息'],
    activeIndex: 0,//选择的下标
    userinfo: {},//用户信息
    page: 1,//页码
    pagingState: false,//分页状态
    articleList: [],//商圈列表
    userID: '',//
  },
  /**关注取下关注 */
  async communityfollow(event) {
    console.log(event);
    let { userinfo,userID } = this.data
    let object = {
      follow_id: userID,
      status: userinfo.is_follow == 0 ? '1' : '0'
    }
    const { code, data } = await getApp().api.communityfollow(object)
    if (code == 1) {
      userinfo.is_follow = userinfo.is_follow == 0 ? '1' : '0'
      this.setData({
        userinfo,
      })
    }
  },
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    console.log(url);
    wx.navigateTo({
      url,
    })
  },
  /**获取商圈列表 */
  async articleLists() {
    let object = {
      page_no: this.data.page,
      cate_id: this.data.tabs[this.data.activeIndex].id,
      user_id: this.data.userID
    }
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.articleLists(object)
      if (code == 1) {
        this.setData({
          articleList: this.data.articleList.concat(data.list).map(item => {
            item.create_time = getApp().friendlyFormatTime(item.create_time * 1000)
            item.showText = false
            return item
          })
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })
        console.log(this.data.page);
      }
    }
  },
  /** 获取分类类别*/
  async communityCate() {
    const { code, data } = await getApp().api.communityCate()
    if (code == 1) {
      this.setData({
        tabs: data
      })
      this.articleLists()
    }
  },
  /**他人信息 */
  async communityUser(user_id) {
    let object = {
      user_id
    }
    const { code, data } = await getApp().api.communityUser(object)
    if (code == 1) {
      this.setData({
        userinfo: data
      })
    }
  },
  navigateBack() {
    wx.navigateBack()
  },
  /**
   * 关闭弹框
   */
  onClose() {
    console.log(1);
    this.setData({ show: false });
  },
  onTabTap(e) {
    console.log(getApp());
    const index = e.currentTarget.dataset.index;
    this.setData({
      activeIndex: index,
      page: 1,//页码
      pagingState: false,//分页状态
      articleList: [],//商圈列表
    });
    this.articleLists()
  },
  /**文本隐藏展示 */
  toggleText(event) {
    console.log(this.data.articleList);
    let { index } = event.currentTarget.dataset
    this.data.articleList[index].showText = !this.data.articleList[index].showText
    this.setData({
      articleList: this.data.articleList,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.setData({
      userID: options.userid
    })
    this.communityCate()
    this.communityUser(options.userid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.articleLists()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onPageScroll(event) {
    this.setData({
      flag: event.scrollTop >= 200 ? true : false
    })
  },
})